<template>
  <view class="cart">
    <!-- 空购物车动态提示 -->
    <u-empty  v-if="goodsList.length===0" mode="car" ></u-empty>
    <!-- 1 列表 -->
    <view class="cart-list">
      <view
        class="cart-item"
        v-for="(item, index) in goodsList"
        :key="item.goods_id"
      >
        <!-- 1 复选框 -->
        <view class="goods-chk">
          <u-checkbox
            :value="item.checked"
            shape="circle"
            active-color="red"
            @change="handleCartToggle(index)"
          ></u-checkbox>
        </view>
        <!-- 2 图片 -->
        <view class="goods-img">
          <u-image
            :src="item.goods_small_logo"
            width="191rpx"
            height="191rpx"
          ></u-image>
        </view>
        <!-- 3 信息 -->
        <view class="goods-info">
          <!-- 名称 -->
          <view class="goods-name u-line-2">{{ item.goods_name }}</view>
          <!-- 价格和数量 -->
          <view class="goods-price-num">
            <view class="goods-price">￥{{ item.goods_price }}</view>
            <view class="goods-num-tool">
              <view class="num-btn" @click="handleNumUpdate(-1, index)">-</view>
              <view class="goods-num">{{ item.num }}</view>
              <view class="num-btn" @click="handleNumUpdate(1, index)">+</view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-chk" @click="change">
        <u-checkbox shape="circle" active-color="red" :value="Checked"  >全选</u-checkbox>
      </view>
      <view class="all-price"> 合计: <text>￥{{totalPrice}}</text> </view>
     
      <view >
         <!-- <u-button type="error" shape="circle" >去结算({{totalNums}})</u-button> -->
         <button class="pay"  @click="handleToPay">去结算({{totalNums}})</button>
         <!-- 去结算({{totalNums}}) -->
         </view>    
    </view>
  </view>
</template>

<script>

// vuex 辅助函数
import { mapState, mapMutations,mapGetters} from "vuex";
export default {
  onLoad() {
    console.log(this.goodsList,'购物车生成');
  },
  computed: {
    ...mapState("cart", ["goodsList"]),//所有需要渲染的数据
    ...mapGetters('cart',['totalPrice','totalNums','Checked']) //引入总价  总数量  是否全选
  },
  methods: {
    ...mapMutations("cart", [
      "cartCheckToggle",
      "cartNumUpdate",
      "cartDeleteOne",
      'changeCheck'
      
    ]),
    // 商品 选中事件
 async   handleCartToggle(index) {
       
       
     await  this.cartCheckToggle(index); // 提交 mutation

      
    },

    change(){  //点击全选事件
        this.changeCheck()

    },
      // 点击 去结算 按钮
    handleToPay() {
      if (this.totalNums === 0) {
        // 没有选购商品
        uni.showToast({
          title: "您还没有选购商品",
          icon: "none",
        });
      } else {
        // 可以跳转到支付页面
        // console.log("可以跳转到支付页面");

        // 跳转页面   方式一： 通过 navigator标签来跳转
        // 跳转页面   方式二： 通过 js代码的方式来跳转

        uni.navigateTo({ url: "/pages/pay/pay" }); // 非tabbar页面
        // uni.switchTab({url:""});// 跳转到 tabbar页面
      }
    },
        // 数量编辑
 async   handleNumUpdate(unit, index) {
      // 把数据 提交给mutation
   // 判断 当前数量 为 1 同时 用户点击 "-"
      if (this.goodsList[index].num === 1 && unit === -1) {
        //  删除
        // uni.showModal({})

        // uni api  支持promise
        // 返回值是数组 [错误信息,真正返回值]
        const [err, { confirm }] = await uni.showModal({
          title: "警告",
          content: "您确定删除吗",
        });
        // 判断用户是否确认删除
        if (confirm) {
          // 要删除
          // 提交mutation 执行删除
          this.cartDeleteOne(index);
        }
      } else {
        // 把数据 提交给mutation
        this.cartNumUpdate({ unit, index }); // 以对象的形式进行传递
      }
    },
  },
};
</script>

<style lang="scss">
.cart {
  padding-bottom: 111rpx;
  .cart-list {
    .cart-item {
      display: flex;
      padding: 10rpx;
      border-bottom: 1rpx solid #ccc;
      .goods-chk {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-img {
        flex: 2;

        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-info {
        color: #666;
        flex: 3;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .goods-name {
        }

        .goods-price-num {
          display: flex;
          justify-content: space-between;
          .goods-price {
            color: #eb4450;
          }

          .goods-num-tool {
            width: 140rpx;
            height: 40rpx;

            display: flex;
            justify-content: space-between;
            .num-btn {
              border: 1rpx solid #ccc;
              border-radius: 50%;
              width: 40rpx;
              height: 40rpx;
              text-align: center;
              line-height: 40rpx;
            }
          }
        }
      }
    }
  }
  .statistics {
    position: fixed;
    background-color: #fff;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 100;
    border-top: 1rpx solid #ccc;
    display: flex;
    align-items: center;
    padding: 15rpx;
    .all-chk {
    }

    .all-price {
      flex: 1;
      color: #666;
      text {
      }
    }

    .all-count {
    }
  }
  .pay{
    color: #fff;
    font-size: 12px;
    border-radius: 10px;
    background-color:#ea4350;
  }
  
}
</style>
